/* eslint-disable no-console */
(function ($) {
    /**
     * 重置系统
     */
    $('#user-settings #reset-default').on('click', async () => {
        try{
            window.setModalAccessibilityState(true); // 禁用模态框
            window.ToastManager.reset({
                title: "是否重置数据?",
                icon: "warning",
                html: `
                    <div class="text-left mt-3">
                        <div class="form-check d-flex flex-row align-items-center">
                            <input class="form-check-input mt-0" type="checkbox" id="keepStats" checked>
                            <label class="form-check-label ms-2" for="keepStats ">
                            保留点击数据（用于标识热门网站）
                            </label>
                        </div>
                        <div class="form-check d-flex flex-row align-items-center">
                            <input class="form-check-input mt-0" type="checkbox" id="keepBgs" checked>
                            <label class="form-check-label ms-2" for="keepBgs">
                            保留本地壁纸
                            </label>
                        </div>
                        <div class="form-check d-flex flex-row align-items-center">
                            <input class="form-check-input mt-0" type="checkbox" id="keepThemes" checked>
                            <label class="form-check-label ms-2" for="keepThemes">
                            保留自定义配色
                            </label>
                        </div>
                        <div class="form-check d-flex flex-row align-items-center">
                            <input class="form-check-input mt-0" type="checkbox" id="keepFavorites" checked>
                            <label class="form-check-label ms-2" for="keepFavorites">
                            保留我的常用
                            </label>
                        </div>
                    </div>
                `,
                preConfirm: () => {
                    return {
                        keepStats: $('#keepStats').is(':checked'),
                        keepBgs: $('#keepBgs').is(':checked'),
                        keepThemes: $('#keepThemes').is(':checked'),
                        keepFavorites: $('#keepFavorites').is(':checked'),
                    }
                }
            }).then((result) => {
                if (result.isConfirmed) {
                    // 1. 获取保留数据
                    const keepStats = result.value.keepStats;
                    const keepBgs = result.value.keepBgs;
                    const keepThemes = result.value.keepThemes;
                    const keepFavorites = result.value.keepFavorites;
                    let successMessage;
                    if (!keepStats && !keepBgs && !keepThemes && keepFavorites) {
                        successMessage = "【全部数据】已清空";
                    } else {
                        const parts = [
                            keepStats ? "点击数据" : "",
                            keepBgs ? "本地壁纸" : "",
                            keepThemes ? "自定义配色" : "",
                            keepFavorites ? "我的常用" : ""
                        ].filter(Boolean); // 过滤空字符串

                        successMessage = `【${parts.join('、')}】被保留`;
                    }

                    const getDefaultExcludedKeys = (options = {}) => {
                        const {
                            keepStats = false,
                            keepThemes = false,
                            keepFavorites = false,
                            customExclusions = []
                        } = options;

                        const excludedKeys = [...customExclusions];

                        if (keepStats) excludedKeys.push('websiteClickStats');
                        if (keepThemes) excludedKeys.push('Themes');
                        if (keepFavorites) excludedKeys.push('myFavorites');

                        return excludedKeys;
                    };

                    // 2. 批量清理
                    window.__cacheManager.clearConfig('default', getDefaultExcludedKeys({
                        keepStats,
                        keepThemes,
                        keepFavorites,
                    }));
                    window.__cacheManager.clearConfig('search');
                    window.__cacheManager.clearConfig('location');
                    window.__cacheManager.clearConfig('weather');
                    if (!keepBgs) {
                        window.__cacheManager.clearConfig('image')
                    } else {
                        console.debug("[IndexedDB] skipped cache group: image")
                    }

                    // 3. 显示成功提示
                    window.setModalAccessibilityState(true); // 禁用模态框
                    window.ToastManager.alert({
                        title: "✅ 重置成功",
                        text: `${successMessage}，页面将刷新...`,
                    }).then(() => {
                        // 4. 刷新页面
                        window.location.reload();
                    });
                }
            });
        } finally {
            window.setModalAccessibilityState(false); // 激活模态框
        }
    })
})(window.jQuery);